package com.blogspot.wanyancan.client.image;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;

// TODO: add these bullshit
/*array1 = new Array("aggregate", "beta-test", "integrate", "capture", "create", "design", "disintermediate", "enable", "integrate", "post", "remix", "reinvent", "share", "syndicate", "tag", "incentivize", "engage", "reinvent", "harness", "integrate");

array2 = new Array("AJAX-enabled","A-list","authentic","citizen-media","Cluetrain","data-driven","dynamic","embedded","long-tail","peer-to-peer","podcasting","rss-capable","semantic","social","standards-compliant","user-centred","user-contributed","viral","blogging","rich-client");

array3 = new Array("APIs","blogospheres","communities","ecologies","feeds","folksonomies","life-hacks","mashups","network effects","networking","platforms","podcasts","value","web services","weblogs","widgets","wikis","synergies","ad delivery","tagclouds");

*/

public class ImagePanel extends Composite {
	
	/**
	 * main panel container
	 */
	final DockPanel mpanel = new DockPanel();

	private final ImageProvider imgProvider = new ImageProvider();
	/**
	 * 
	 * @author billy
	 */
	public class ImageProvider implements DynaTableDataProvider {

		public ImageProvider() {
			// Initialize the service.
			//
			imgService = (ImageServiceAsync) GWT
					.create(ImageService.class);

			ServiceDefTarget target = (ServiceDefTarget) imgService;
			target.setServiceEntryPoint(GWT.getModuleBaseURL()+"/imageservice");
		}

		public void updateRowData(final int startRow, final int numRows,
				final RowDataAcceptor acceptor) {
			// Check the simple cache first.
			//
			if (startRow == lastStartRow) {
				if (numRows == lastNumRows) {
					// Use the cached batch.
					//
					pushResults(acceptor, startRow, lastImage);
					return;
				}
			}

			// Actually fetch the data remotely.
			//
			imgService.getImage(startRow, numRows, new AsyncCallback() {
				public void onFailure(Throwable caught) {
					acceptor.failed(caught);
				}

				public void onSuccess(Object result) {
					ImageRec[] images = (ImageRec[]) result;
					lastStartRow = startRow;
					lastNumRows = numRows;
					lastImage = images;
					pushResults(acceptor, startRow, images);
				}
			});
		}

		private void pushResults(RowDataAcceptor acceptor, int startRow,
				ImageRec[] images) {
/*			String[][] rows = new String[people.length][];
			for (int i = 0, n = rows.length; i < n; i++) {
				ImageRec person = people[i];
				rows[i] = new String[3];
				rows[i][0] = person.getName();
				rows[i][1] = person.getDescription();
				rows[i][2] = "";
			}*/
			acceptor.accept(startRow, images);
		}

		public final ImageServiceAsync imgService;

		private int lastStartRow = -1;
		private int lastNumRows = -1;

		/**
		 * cached last displayed Image records
		 */
		private ImageRec[] lastImage;
	}

	public ImagePanel(int visibleRows) {
		dynaTable = new DynaTableWidget(imgProvider, visibleRows);
		mpanel.add(dynaTable, DockPanel.CENTER);
		initWidget(mpanel);
	}

	protected void onLoad() {
		dynaTable.refresh();
	}

	protected void setDayIncluded(int day, boolean included) {
		if (daysFilter[day] == included) {
			// No change.
			//
			return;
		}

		daysFilter[day] = included;
		if (pendingRefresh == null) {
			pendingRefresh = new Command() {
				public void execute() {
					pendingRefresh = null;
					dynaTable.refresh();
				}
			};
			DeferredCommand.add(pendingRefresh);
		}
	}

	private final boolean[] daysFilter = new boolean[] { true, true, true,
			true, true, true, true };

	private final DynaTableWidget dynaTable;

	private Command pendingRefresh;
}
